package annotationtest.customannotations;

import java.lang.reflect.Field;

public class FruitInfoUtil {
	public static void getFruitInfo(Class<?> clazz) {
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			if (field.isAnnotationPresent(FruitName.class)) {
				String strFruitName = "水果名称：" + getFruitName(field);
				System.out.println(strFruitName);
			} else if (field.isAnnotationPresent(FruitColor.class)) {
				String strFruitColor = " 水果颜色：" + getFruitColor(field);
				System.out.println(strFruitColor);
			} else if (field.isAnnotationPresent(FruitProvider.class)) {
				String strFruitProvicer = getFruitProvider(field);
				System.out.println(strFruitProvicer);
			}
		}
	}

	public static void init(Fruit fruit,Class<?> clazz) throws IllegalArgumentException, IllegalAccessException {
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			field.setAccessible(true);
			if (field.isAnnotationPresent(FruitName.class)) {
				field.set(fruit, getFruitName(field));
			} else if (field.isAnnotationPresent(FruitColor.class)) {
				field.set(fruit, getFruitColor(field));
			} else if (field.isAnnotationPresent(FruitProvider.class)) {
				field.set(fruit, getFruitProvider(field));
			}
		}
	}
	
	private static String getFruitName(Field field) {
		FruitName fruitName = (FruitName) field.getAnnotation(FruitName.class);
		return fruitName.value().toString();
	}

	private static String getFruitColor(Field field) {
		FruitColor fruitColor = (FruitColor) field
				.getAnnotation(FruitColor.class);
		return fruitColor.fruitColor().toString();
	}

	private static String getFruitProvider(Field field) {
		FruitProvider fruitProvider = (FruitProvider) field
				.getAnnotation(FruitProvider.class);
		String fruitProviderInfo = "供应商信息： 供应商编号：" + fruitProvider.id()
				+ " 供应商名称：" + fruitProvider.name() + " 供应商地址："
				+ fruitProvider.address();
		return fruitProviderInfo.toString();
	}
}
